<?php echo $this->render('/public/header'); ?>
    <script src="https://cdn.bootcss.com/echarts/3.8.5/echarts.min.js"></script>
<?php use yii\helpers\Url; ?>
    <div id="main" class="col-xs-12">
        <div class="form-inline">
            <form action="" method="get">
                <a href="<?php echo Url::to(['setting/inquiry']); ?>" class="btn btn-success">添加</a>
                <select class="form-control" name="refer">
                    <option value="0">请选择来源</option>
                    <?php foreach ($this->params['inquiryType'] as $v): ?>
                        <option value="<?php echo $v['id']; ?>" <?php if (yii::$app->request->get('refer') == $v['id']) {
                            echo 'selected';
                        } ?> ><?php echo $v['name']; ?></option>
                    <?php endforeach; ?>
                </select>
                <input placeholder="开始时间" type="text" value="<?php echo yii::$app->request->get('begin_time'); ?>"
                       class="form-control" style="width:100px"
                       onclick="layui.laydate({elem: this})" name="begin_time"/>

                <input placeholder="结束时间" type="text" value="<?php echo yii::$app->request->get('end_time'); ?>"
                       class="form-control" style="width:100px"
                       onclick="layui.laydate({elem: this})" name="end_time"/>
                <select class="form-control" name="star">
                    <option value="0" <?php if (yii::$app->request->get('star') == 0) {
                        echo 'selected';
                    } ?>>--客户星级--
                    </option>
                    <option value="1" <?php if (yii::$app->request->get('star') == 1) {
                        echo 'selected';
                    } ?>>1星
                    </option>
                    <option value="2" <?php if (yii::$app->request->get('star') == 2) {
                        echo 'selected';
                    } ?>>2星
                    </option>
                    <option value="3" <?php if (yii::$app->request->get('star') == 3) {
                        echo 'selected';
                    } ?>>3星
                    </option>
                    <option value="4" <?php if (yii::$app->request->get('star') == 4) {
                        echo 'selected';
                    } ?>>4星
                    </option>
                    <option value="5" <?php if (yii::$app->request->get('star') == 5) {
                        echo 'selected';
                    } ?>>5星
                    </option>
                </select>
                <select name="recommend" class="form-control" style="width:100px">
                    <option value=0>--客户来源--</option>
                    <?php foreach ($this->params['customerRefer'] as $v2): ?>
                        <option value="<?php echo $v2['id']; ?>"><?php echo $v2['name']; ?></option>
                    <?php endforeach; ?>
                </select>
                <select class=" form-control
                    " name="service_name">
                    <option value=0>--全部业务员--</option>
                    <?php foreach ($this->params['staff_list'] as $k => $v) { ?>
                        <option <?php if (yii::$app->request->get('service_name') == $v['job_number']) {
                            echo 'selected';
                        } ?> value="<?php echo $v['job_number']; ?>"><?php echo $v['name']; ?></option>
                    <?php } ?>
                </select>
                <select class="form-control" name="status">
                    <option value=0>--全部状态--</option>
                    <option value=1 <?php if (yii::$app->request->get('status') == 1) {
                        echo 'selected';
                    } ?>>待分配
                    </option>
                    <option value=2 <?php if (yii::$app->request->get('status') == 2) {
                        echo 'selected';
                    } ?>>已分配
                    </option>
                    <option value=3 <?php if (yii::$app->request->get('status') == 3) {
                        echo 'selected';
                    } ?>>拒绝
                    </option>
                </select>
                <select class="form-control" name='property'>
                    <option value=0>--选择定单属性--</option>
                    <option value=1 <?php if (yii::$app->request->get('property') == 1) {
                        echo 'selected';
                    } ?>>未下单
                    </option>
                    <option value=2 <?php if (yii::$app->request->get('property') == 2) {
                        echo 'selected';
                    } ?>>样品单
                    </option>
                    <option value=3 <?php if (yii::$app->request->get('property') == 3) {
                        echo 'selected';
                    } ?>>已下单
                    </option>
                </select>
                <input value="<?php echo yii::$app->request->get('customer_name'); ?>" type="text" name="customer_name"
                       class="form-control" style="margin-left:10px;width:100px" placeholder="搜索用户"/>
                <input value="<?php echo yii::$app->request->get('email'); ?>" type="text" name="email"
                       class="form-control" style="margin-left:10px;width:150px" placeholder="邮箱"/>
                <input value="<?php echo yii::$app->request->get('inquiry_detail'); ?>" type="text"
                       name="inquiry_detail" class="form-control" style="margin-left:10px;width:150px"
                       placeholder="搜索询盘详情"/>
                <button class="btn btn-success" style="margin-left:10px">搜索</button>
            </form>
            <br/>
            <button onclick="download()" class="btn btn-success">下载excel</button>
            <button onclick="showchart()" class="btn btn-success">显示图表</button>
        </div>
        <br/>
        <br/>
        <div id="table">
            <table class="table table-borderd">
                <thead>
                <tr>
                    <th style="width:50px">id</th>
                    <th>询盘来源</th>
                    <th>新旧询盘</th>
                    <th>客户编号</th>
                    <th>客户名称</th>
                    <th>咨询时间</th>
                    <th>回复时间</th>
                    <th>分配时间</th>
                    <th style="width:50px">国家</th>
                    <th style="width:200px">email</th>
                    <!-- <th style="width:200px">下单email</th> -->
                    <th style="width:100px">订单属性</th>
                    <th>客户来源</th>
                    <th style="width:400px">询盘内容</th>
                    <th style="width:50px">客户等级</th>
                    <th>业务员</th>
                    <th>状态</th>
                    <th style="width:100px">操作</th>
                </tr>
                </thead>
                <tbody>
                <?php foreach ($this->params['list'] as $k => $v) { ?>
                    <tr>
                        <td><?php echo $v['id']; ?></td>
                        <td>
                            <?php foreach ($this->params['inquiryType'] as $v2): ?>
                                <?php if ($v2['id'] == $v['refer']): ?>
                                    <?php echo $v2['name']; ?>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </td>
                        <td><?php echo $v['inquiry_status'] == 1 ? '新客户' : '老客户'; ?></td>
                        <td><?php echo $v['customer_num']; ?></td>
                        <td><?php echo $v['name']; ?></td>
                        <td><?php if ($v['consultation_time']) {
                                echo date('Y-m-d', $v['consultation_time']);
                            } ?></td>
                        <td><?php if ($v['reply_time']) {
                                echo date('Y-m-d', $v['reply_time']);
                            } ?></td>
                        <td><?php if ($v['dispatch_time']) {
                                echo date('Y-m-d', $v['dispatch_time']);
                            } ?></td>
                        <td><?php echo $v['country']; ?></td>
                        <td style="width:200px"><?php echo $v['email']; ?></td>
                        <!-- <td style="width:200px"><?php echo $v['order_email']; ?></td> -->
                        <td>
                            <?php if ($v['property'] == 1) {
                                echo '未下单';
                            } elseif ($v['property'] == 2) {
                                echo '样品单';
                            } elseif ($v['property'] == 3) {
                                echo '已下单';
                            } ?>
                        </td>
                        <td>
                            <?php foreach ($this->params['customerRefer'] as $v2): ?>
                                <?php if ($v2['id'] == $v['recommend']): ?>
                                    <?php echo $v2['name']; ?>
                                <?php endif; ?>
                            <?php endforeach; ?>
                        </td>
                        <td style="width:200px"><?php echo $v['inquiry_detail']; ?></td>
                        <td><?php echo $v['star']; ?>星</td>
                        <td><?php echo $v['sale_name']; ?></td>
                        <td>
                            <?php if (1 == $v['status']) {
                                $a = "未分配";
                            } elseif (2 == $v['status']) {
                                $a = "已分配";
                            } elseif (3 == $v['status']) {
                                $a = '拒绝';
                            }
                            echo $a; ?>
                        </td>
                        <td style="width:100px">
                            <a href="<?php echo Url::to(['setting/inquiry_edit', 'id' => $v['id']]); ?>"
                               class="layui-btn layui-btn-mini layui-btn-normal">编辑</a>
                            <a href="javascript:void(0)" onclick="del_inquiry(<?php echo $v['id']; ?>)"
                               class="layui-btn layui-btn-mini layui-btn-danger">删除</a></td>
                    </tr>
                <?php } ?>
                </tbody>
            </table>
            <div id="page"></div>
        </div>
        <div id="chart" style="width:100%;min-width:1200px;height:700px;display:none">

        </div>
    </div>
    <script>
        var myChart = echarts.init(document.getElementById('chart'));

        // 指定图表的配置项和数据
        var option = {
            title: {
                text: '询盘来源统计',
                x: 'center'
            },
            tooltip: {
                trigger: 'item',
                formatter: "{a} <br/>{b}: {c} ({d}%)"
            },
            legend: {
                orient: 'vertical',
                x: 'left',
                data:<?php echo json_encode($title);?>
            },
            series: [
                {
                    name: '访问来源',
                    type: 'pie',
                    radius : '55%',
                    center: ['50%', '60%'],
                    data:<?php echo json_encode($chart);?>,
                    itemStyle: {
                        emphasis: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);
    </script>
    <script>
        function showchart() {
            $("#table").toggle();
            $("#chart").toggle();
            myChart.resize();
        }

        function download() {
            var url = '/setting/download-inquiry-excel' + location.search;
            window.location.href = url;
        }

        function del_inquiry(v) {
            if (layer.confirm('确认删除', {}, function () {
                    var url = "<?php echo Url::to(['setting/del_inquiry']);?>";
                    $.post(url, {id: v}, function (res) {
                        if (res.code == 1) {
                            layer.confirm(res.msg, {}, function () {
                                window.location.reload();
                            })
                        }
                        else {
                            layer.alert(res.msg);
                        }
                    }, 'json')
                })) ;
        }

        layui.laypage({
            cont: 'page'
            , pages: <?php echo $this->params['page']['page'];?>
            , curr:<?php echo $this->params['page']['curr'];?>
            , skin: '#5FB878'
            , skip: true
            , jump: function (obj, first) {
                if (!first) {
                    if (window.location.search) {
                        window.location.href = "<?php echo $this->params['page']['base_url'];?>&p=" + obj.curr;
                    }
                    else {
                        window.location.href = "<?php echo $this->params['page']['base_url'];?>?p=" + obj.curr;
                    }

                }
            }
        });
    </script>
<?php echo $this->render('/public/footer'); ?>